package cn.dzh.controller;

import cn.dzh.model.bean.CommodityBase;
import cn.dzh.model.bean.CommodityDraft;

import cn.dzh.model.dto.CommodityBaseDTO;
import cn.dzh.model.dto.HttpResult;
import cn.dzh.service.CommodityDraftService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 聂海波
 * @version 1.1
 * @date 2023/10/24 11:36
 */
@Slf4j
@RestController
@RequestMapping("/commodity")
public class CommodityDraftController {
     @Resource
     private CommodityDraftService commodityDraftService;

    @GetMapping("/comebackdata")
    public HttpResult comebackData(int id){
        log.info("id====>{}",id);
        CommodityDraft commodityDraft=commodityDraftService.findCommodityDraft(id);
        if (commodityDraft==null){
            return HttpResult.error(1,"没有找到相关信息");
        }
        return HttpResult.ok("商品信息",commodityDraft);
    }

    @PostMapping("/update")
    public HttpResult updateGoods( @Validated CommodityDraft commodityDraft) {
        HttpResult result;
        int id=commodityDraft.getGoodsId();
        String status = commodityDraftService.selectStatus(id);
        if(status==null){
            return HttpResult.error(1,"商品不存在，请重新输入");
        }
        if("已下线".equalsIgnoreCase(status)){
            return HttpResult.ok("商品已下线，不可以编辑");
        }
        int d = commodityDraftService.updateGoodsById(commodityDraft);
        if (d > 0) {
            result = HttpResult.ok("修改成功");
        } else {
            result = HttpResult.error(1, "修改失败");
        }
        return result;
    }
    @PostMapping("/selectBase")
    public HttpResult selectBase( CommodityBaseDTO commodityBasedto){
        Integer pageNum=commodityBasedto.getPageNum()==null?1:commodityBasedto.getPageNum();
        Integer pageSize=commodityBasedto.getPageSize()==null?10:commodityBasedto.getPageSize();
        //将CommodityBaseDTO转为commodityBase
        CommodityBase commodityBase=new CommodityBase();
        commodityBase.setGoodsName(commodityBasedto.getGoodsName());
        commodityBase.setGoodsId(commodityBasedto.getGoodsId());
        commodityBase.setOnlineStatus(commodityBasedto.getOnlineStatus());
        commodityBase.setAuditResult(commodityBasedto.getAuditResult());
        PageHelper.startPage(pageNum,pageSize);
        List<CommodityBase> commodityBases = commodityDraftService.selectBase(commodityBase);
        PageInfo<CommodityBase> list = new PageInfo<>(commodityBases);
        return HttpResult.ok("查询成功",list);
    }
    @GetMapping("/selectcommoditybyid")
    private HttpResult selectById(Integer id){
      //List list=commodityDraftService.selectById(id);
        return HttpResult.ok("查看成功");
    }


    @PostMapping("/addToDraft")
    public HttpResult addToDraft(@RequestBody CommodityDraft commodityDraft) {
        System.out.println(commodityDraft);
        int i = commodityDraftService.addToDraft(commodityDraft);
        if (i>0){
            return HttpResult.ok("商品草稿添加成功");
        }else{
            return HttpResult.error(1122,"商品草稿添加失败");
        }

    }
}
